package com.iflysse.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iflysse.dao.QuestionDao;
import com.iflysse.pojo.bo.req.KnowledgePointBoReq;
import com.iflysse.pojo.bo.req.QuestionIdListBoReq;
import com.iflysse.pojo.bo.resp.QuestionBoResp;
import com.iflysse.pojo.bo.resp.QuestionListBoResp;
import com.iflysse.pojo.bo.resp.QuestionPlainBoResp;
import com.iflysse.pojo.bo.resp.QuestionPlainListBoResp;
import com.iflysse.pojo.po.req.QuestionIdBoReq;
import com.iflysse.pojo.po.resp.QuestionPoResp;
import com.iflysse.services.QuestionService;
import com.iflysse.util.CommonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    public static final Logger log = Logger.getLogger(ActivityServiceImpl.class);
    @Autowired
    QuestionDao questionDao ;
    /**
     * 根据3级知识点获取问题,知识点等级:0,1,2
     *
     * @param knowledgePointBoReq
     * @return
     */
    @Override
    public QuestionPlainListBoResp loadQuestionByL2KnowledgePoint(KnowledgePointBoReq knowledgePointBoReq) throws SQLException {
        //根据知识点编号获取问题列表
        List<QuestionPoResp> questionPoRespList = questionDao.getQuestionByKnowledgePointId(knowledgePointBoReq.getKnowledgePointId());
        QuestionPlainListBoResp questionPlainListBoResp = new QuestionPlainListBoResp();
        questionPlainListBoResp.setLoadQuestionByL2KnowledgePoint(new ArrayList<>());
        //将问题添加到问题集合
        questionPoRespList.forEach(questionPoResp -> {
            QuestionPlainBoResp questionPlainBoResp = CommonUtil.objToObj(questionPoResp, QuestionPlainBoResp.class);
            //将数据库的json提取出title
            JSONObject jsonObject = JSON.parseObject(questionPoResp.getQuestionContent());
            String questionTitle = jsonObject.getString("titleContent");
            questionPlainBoResp.setQuestionTitle(questionTitle);
            questionPlainListBoResp.getLoadQuestionByL2KnowledgePoint().add(questionPlainBoResp);
        });
        return questionPlainListBoResp;
    }

    /**
     * 根据问题编号获取问题
     *
     * @param questionIdBoReq
     * @return
     */
    @Override
    public QuestionBoResp getQuestionByQuestionId(QuestionIdBoReq questionIdBoReq) throws SQLException {
        //问题详情
        QuestionPoResp questionPoResp = questionDao.getQuestionByQuestionId(questionIdBoReq.getQuestionId());
        //数据库对象转service对象
        QuestionBoResp questionBoResp = CommonUtil.objToObj(questionPoResp, QuestionBoResp.class);
        return questionBoResp;
    }

    /**
     * 根据问题编号列表获取问题集合
     *
     * @param questionIdListBoReq  问题编号列表
     * @return 问题集合
     */
    @Override
    public QuestionListBoResp getQuestionByQuestionIdList(QuestionIdListBoReq questionIdListBoReq) throws SQLException {


        QuestionListBoResp questionListBoResp = new QuestionListBoResp();
        //查询的id集合有数据
        if(questionIdListBoReq.getQuestionIds().size() >0) {

            List<QuestionPoResp> questionList = questionDao.getQuestionListByQuestionIds(questionIdListBoReq.getQuestionIds());

            questionList.forEach(questionPoResp -> {
                questionListBoResp.getQuestionBoRespList().add(CommonUtil.objToObj(questionPoResp, QuestionBoResp.class));
            });
        }
        return questionListBoResp;
    }


}
